package com.yytek.nmp.mqtt;

import com.yytek.common.client.base.MqMessage;
import com.yytek.common.client.net.IotMqttClient;
import com.yytek.common.client.net.IotMqttContext;
import com.yytek.common.client.net.MqttCallback;
import com.yytek.common.core.constant.MqttConstants;
import com.yytek.nmp.service.impl.IotDeviceStateServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 设备状态业务相关
 */
@Service
@Slf4j
public class DeviceStateMqttService implements MqttCallback {

    @Resource
    IotMqttClient iotMqttClient;

    @Resource
    private IotDeviceStateServiceImpl iotdeviceStateService;

    @PostConstruct
    public void init() {
        //订阅
        iotMqttClient.subscribe(MqttConstants.NMP_STATE_TOPIC,this);
    }
    @Override
    public void onMessageReceived(IotMqttContext context, MqMessage msg) {
        //业务处理
//        System.out.println("---topic----"+context.getTopic());
//        System.out.println("---getProductId----"+context.getProductId());
//        System.out.println("---getDeviceName----"+context.getDeviceName());
//        System.out.println("---msgid----"+msg.getMessageId());
//        System.out.println("---msg----"+msg.getJsonObject().toJSONString());
        //处理状态业务
        iotdeviceStateService.changeDeviceStatusByMsg(msg);
    }


}
